﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GestorDeFlotasDesktop.Classes;
using System.Data.SqlClient;
namespace GestorDeFlotasDesktop.AbmTurno
{
    public partial class AbmTurno : Form
    {
        private GestorDeFlotasDesktop.AbmTurno.frmNuevoTurno frameNuevoTurno;

        public AbmTurno()
        {
            InitializeComponent();
        }
        private void buscarTurnos()
        {
            buscarTurnos("");
        }
        private void buscarTurnos(string descripcion)
        {
            SqlInterface.Conectar();
            Turnos oTurnos = new Turnos();
            dataGridView1.DataSource = oTurnos.buscarTurno(descripcion);
            SqlInterface.Desconectar();
        }
        private void frmAbmTurno_Load(object sender, EventArgs e)
        {
            buscarTurnos();

        }

        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            txtBusqueda.Text = "";
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            frameNuevoTurno = new GestorDeFlotasDesktop.AbmTurno.frmNuevoTurno();
            frameNuevoTurno.MdiParent = this.MdiParent;
            frameNuevoTurno.Show();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            buscarTurnos(txtBusqueda.Text);
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            Turno oTurno = (Turno)dataGridView1.CurrentRow.DataBoundItem;
            
            SqlInterface.Conectar();
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@codigo", ((Turno)dataGridView1.CurrentRow.DataBoundItem).intCodigo));
            
            if (SqlInterface.ExecuteNonQuery("SQUIRTLE.spBajaTurno", parameterList) != 1)
            {
                MessageBox.Show("Error en eliminacion de turno");
            }
            else
            {
                MessageBox.Show("El turno se ha eliminado satisfactoriamente.");
            }
            buscarTurnos();

            SqlInterface.Desconectar();
        }

        private void btnModificar_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow != null)
            {
                frameNuevoTurno = new GestorDeFlotasDesktop.AbmTurno.frmNuevoTurno();
                frameNuevoTurno.MdiParent = this.MdiParent;
                frameNuevoTurno.cargarModificar((Turno)dataGridView1.CurrentRow.DataBoundItem);
                frameNuevoTurno.Show();
            }
        }

        private void AbmTurno_FormClosing(object sender, FormClosingEventArgs e)
        {
            Turnos oTurnos = new Turnos();
            SqlInterface.Conectar();
            int result = oTurnos.getVerificarTurnos();
            if (result != 24)
            {
                result = 24 - result;
                MessageBox.Show("Faltan cubrir " + result + " horas.", "Atención", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
           
            SqlInterface.Desconectar();
        }
    }
}
